﻿using System;
using System.Linq;
using System.Text.Json.Serialization;

using Xc.DataAccess.Core;

namespace Xc.DataAccess.OneNetCore.Services
{
    public class RespondResult
    {
        /// <summary>
        /// 是否错误
        /// </summary>
        [JsonPropertyName("errno")]
        public int ErrorCode { get; set; }
        /// <summary>
        /// 判断是否成功
        /// </summary>
        [JsonIgnore]
        public bool IsSuccess => ErrorCode == 0;
        [JsonIgnore]
        public bool IsError => !IsSuccess;
        /// <summary>
        /// 消息内容
        /// </summary>
        [JsonPropertyName("error")]
        public string Message { get; set; }
        public static implicit operator Result(RespondResult respondResult)
        {
            if (respondResult.IsSuccess)
            {
                return Result.Accept(respondResult.Message);
            }
            else
            {
                return Result.Reject(respondResult.Message);
            }
        }
    }
    public class RespondResult<T>: RespondResult
    {

        /// <summary>
        /// 数据内容
        /// </summary>
        [JsonPropertyName("data")]
        public T Data { get; set; }

        public static implicit operator ResultData<T>(RespondResult<T> respondResult)
        {
            if (respondResult.IsSuccess)
            {
                return ResultData<T>.Accept(respondResult.Message,respondResult.Data);
            }
            else
            {
                return ResultData<T>.Reject(respondResult.Message, respondResult.Data);
            }
        }
    }
}
